function listToChat(toUserid){
    var userid = androidFun.getUserId();
    var token = androidFun.getToken();
    $.post("http://39.108.97.134:8886/wlzz/getInfo/useridGetForInfo",{token:token,userid:toUserid},function(data) {
        var obj = eval('(' + data + ')');
        if(obj.code==0){
            androidFun.newPage("与"+obj.data.nickName+"聊天","http://39.108.97.134:8801/wlzzim/index/chat?toUserid="+toUserid+"&userId="+userid);
        }else{
            androidFun.showAndroidToast("该用户异常无法与他聊天");
        }
    });
}
function no_read_msg(data){
    var obj = eval('(' + data + ')');
    if(obj.data==""){
        console.log("无未读信息需要操作");
    }else{
        var dataList = obj.data;
       for(var i = 0;i<dataList.length;i++){
           document.getElementById("msg_number_"+dataList[i].userid).innerHTML= '<span class="bui-badges"></span>';
       }
    }
}
var DemoHandler = function () {
  this.onopen = function (event, ws) {
      // ws.send('hello 连上了哦')
      console.log("连接成功");
  }

  /**
   * 收到服务器发来的消息
   * @param {*} event 
   * @param {*} ws 
   */

  this.onmessage = function (event, ws) {
	  var data = event.data;
      console.log(data);
      var obj = eval('(' + data + ')');
      if(obj.flag==10){  //这是会话列表的内容
          var dataList = obj.data;
          var allContent = "";
          for(var i =0;i<dataList.length;i++){
              var msg = "";
              if(dataList[i].dialogueList.data.content.length-0>16){
                  msg = dataList[i].dialogueList.data.content.substr(0,16)+"...";
              }else{
                  msg = dataList[i].dialogueList.data.content;
              }
             var content = ' <li id="list_'+dataList[i].dialogueList.userid2+'">'+
              '                      <div class="bui-btn bui-box listToChat" ontouchstart="longTouch(\''+dataList[i].dialogueList.userid2+'\')" ontouchend="nolongTouch()" onclick="listToChat(\''+dataList[i].dialogueList.userid2+'\')">'+
              '                        <img src="'+getHeadImg(dataList[i].imgUrl)+'" style="width: 45px;height: 45px">'+
              '                                        &nbsp;&nbsp;&nbsp;<div class="span2">'+dataList[i].nickname+'<br><span id="bestNews_'+dataList[i].dialogueList.userid2+'" class="bestNews">'+msg+'</span></div><div id="beastNewsCount_'+dataList[i].dialogueList.userid2+'"></div> '+
              '<div id="msg_number_'+dataList[i].dialogueList.userid2+'"></div>'+
              '                                    </div>'+
              '                                </li>';
             allContent = allContent + content;
          }
          document.getElementById("listview").innerHTML=allContent;
          layer.closeAll();
      }
      else if(obj.flag==0){  //未读消息
          setTimeout('no_read_msg(\''+data+'\')',"200");//延时加载
      }
      else if(obj.flag==1){
          var array=new Array(0,300,50,100);
          androidFun.vibrate(-1,array);
          if(document.getElementById("list_"+obj.userid)==null){
               // var token = androidFun.getToken();
               // var msg = "";
               // if(obj.content.length-0>16){
               //    msg = obj.content.substr(0,16)+"...";
               // }else{
               //     msg = obj.content;
               // }
               // $.post("http://39.108.97.134:8886/wlzz/getInfo/useridGetForInfo",{token:token,userid:obj.userid},function(event){
               //     if(event.code==0){
               //         var content = ' <li id="list_'+event.data.userId+'">'+
               //             '                      <div class="bui-btn bui-box listToChat" ontouchstart="longTouch(\''+event.data.userId+'\')" ontouchend="nolongTouch()" onclick="listToChat(\''+event.data.userId+'\')">'+
               //             '                        <img src="'+getHeadImg(data.data.imgUrl)+'" style="width: 45px;height: 45px">'+
               //             '                                        &nbsp;&nbsp;&nbsp;<div class="span2">'+event.data.nickName+'<br><span id="bestNews_'+event.data.userId+'" class="bestNews">'+msg+'</span></div><div id="beastNewsCount_'+event.data.userId+'"></div> '+
               //             '<div id="msg_number_'+event.data.userId+'"></div>'+
               //             '                                    </div>'+
               //             '                                </li>';
               //         document.getElementById("listview").innerHTML= content + document.getElementById("listview").innerHTML;
               //       }
               // });
              setTimeout('tiows.reconn()',"100");//延时加载
          //    tiows.reconn();
          }else{
              document.getElementById("msg_number_"+obj.userid).innerHTML= '<span class="bui-badges"></span>';
              if(obj.content.length-0>15){
                  document.getElementById("bestNews_"+obj.userid).innerHTML= obj.content.substr(0,16)+"...";
              }else{
                  document.getElementById("bestNews_"+obj.userid).innerHTML= obj.content;
              }
              var listContent = ' <li id="list_'+obj.userid+'">'+document.getElementById("list_"+obj.userid).innerHTML+ '</li>';
              var list_ = document.getElementById("list_"+obj.userid);
              list_.parentNode.removeChild(list_);
              document.getElementById("listview").innerHTML =  listContent+document.getElementById("listview").innerHTML;
          }
      }
      layer.closeAll();
      // bui.ready(function () {
      //     $(".menuToDialogue").on("click", function (argument) {
      //         bui.load({
      //             url: "/wlzzim/index/dialogue",
      //         })
      //     })
      //     $(".menuToMyFriends").on("click", function (argument) {
      //         bui.load({
      //             url: "/wlzzim/index/contacts",
      //         })
      //     });
      //     // var tab = bui.slide({
      //     //     id: "#tabNews",
      //     //     menu: "#tabNav",
      //     //     children: ".bui-tab-main > ul",
      //     //     scroll: true,
      //     //     index: 1
      //     // });
      // });
  }

  this.onclose = function (e, ws) {
      console.log("连接关闭");
  }

  this.onerror = function (e, ws) {
      console.log("连接发生错误关闭");
  }

  /**
   * 发送心跳，本框架会自动定时调用该方法，请在该方法中发送心跳
   * @param {*} ws 
   */
  this.ping = function (ws) {
    // log("发心跳了")
    ws.send('心跳内容')
  }
}
